home *** CD-ROM | disk | FTP | other *** search
- /* findmax.c --- p 110 Bible */
- #include <stdio.h>
- #include <stdarg.h>
- int findmax(int, ...);
- main()
- {
- int maxvalue;
- /* The end of the list of integers
- is marked by -9999 */
- maxvalue = findmax(-1, 20, 30, 50, -9999);
- printf("findmax(-1, 20, 30, 50, -9999) returns: %d\n", maxvalue);
- maxvalue = findmax(1, 2, 3, 4, 5, 6, 7, 8, -9999);
- printf("findmax(1, 2, 3, 4, 5, 6, 7, 8, -9999) returns: %d\n", maxvalue);
- }
- /*-----------------------------------------------*/
- /* The "findmax" finds the largest value in a list
- * of integers. It uses the "va_..." macros to get
- * the arguments. This is ANSI version. */
- int findmax(int firstint, ...)
- {
- int maxval = -9999, x = 0;
- va_list argp;
- /* Get the first optional parameter using "va_start" */
- va_start(argp, firstint);
- x = firstint;
- while(x != -9999) /* -9999 marks end of arguments */
- {
- if(maxval < x) maxval = x;
- x = va_arg(argp, int);
- }
- return (maxval);
- }